package com.eascs.ticket.ticket.service;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eascs.ticket.common.entity.StatusConstant;
import com.eascs.ticket.common.service.BaseSevice;
import com.eascs.ticket.ticket.entity.TicketAttachment;
import com.eascs.ticket.ticket.entity.TicketAttachmentDelete;

@Service
public class TicketAttachmentService extends BaseSevice {
	@Inject
	private TicketChangeLogService ticketChangeLogService;

	public List<Object[]> getTicketAttachmentByTicketId(String ticketId) {
		return this.proxyDao.findObjectsList("select ta.id, a.name,a.file_size,ta.updator,ta.update_date from ticket_attachment ta left join attachments a on a.id=ta.attachment_id where ta.ticket_id=?  and ta.status<> ? order by ta.create_date", ticketId, StatusConstant.STATUS_DEL);
	}

	@Transactional(transactionManager = "ticketTransactionManager")
	public void insertTicketAttachment(TicketAttachment ticketAttachment) {
		this.proxyDao.insert(ticketAttachment);
		ticketChangeLogService.insertTicketChangeLogWithObject(ticketAttachment.getTicketId(), "增加附件", ticketAttachment);
	}

	@Transactional(transactionManager = "ticketTransactionManager")
	public void deleteTicketAttachment(TicketAttachmentDelete ticketAttachmentDelete) {
		TicketAttachment ticketAttachment = this.getTicketAttachmentById(ticketAttachmentDelete.getId());
		ticketChangeLogService.insertTicketChangeLogWithObject(ticketAttachment.getTicketId(), "删除附件", ticketAttachmentDelete);
		this.proxyDao.updateById(ticketAttachmentDelete);
	}

	public TicketAttachment getTicketAttachmentById(String id) {
		return this.proxyDao.getById(TicketAttachment.class, id);
	}
}
